// pages/document/list.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    keyword: "",
    searchPlaceholder: "",
    page: 1,
    dataList: [],
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
    isTouchstart: false,
    isLongtap: false,
    isTouchmove: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    Object.assign(this, ImgListCell);
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    /**
     * 显示 loading
     */
    that.setData({
      loadinghidden: false
    });
    that.doLoadData();
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成
    // 页面渲染完成
    var that = this;

    // 数据加载完成后 延迟隐藏loading
    setTimeout(function() {
      that.setData({
        loadinghidden: true
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  doLoadData: function() {
    var that = this;

    var parameter = {};
    parameter['page'] = 1;
    parameter['keyword'] = that.data.keyword;

    http.ApiGet(apiUrl.UrlDocumentList, parameter, function(res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          newData = newData.map(function(item) {
            item.ptime = item.create_time_text + " | 采:" + item.used_count; // util.getDateDiff(item.ptime + '000');
            // if (item.user_name != null) {
            //   item.user_name = item.user_name.substring(0, 8);
            // } else {
            //   item.user_name = "";
            // }
            item.autor = item.user_name;
            if (item.images.length > 0) {
              item.thumb = item.images;
            }

            return item;
          })
        } else {
          newData = [];
          // wx.showModal({
          //   title: "暂无数据！"
          // });
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: newData
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.doLoadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['page'] = page;
    parameter['keyword'] = that.data.keyword;

    http.ApiGet(apiUrl.UrlDocumentList, parameter, function(res) {

      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {

          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function(item) {
              item.ptime = item.create_time_text + " | 采:" + item.used_count;
              item.autor = item.user_name;
              if (item.images.length > 0) {
                item.thumb = item.images;
              }

              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  searchContentInput: function(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  searchSubmit: function() {
    var that = this;
    var keyword = that.data.keyword;
    // if (keyword != "") {
    //   console.log(keyword);
    //   that.doLoadData();
    // } else {
    //   wx.showModal({
    //     title: "请输入搜索关键字！"
    //   });
    // }
    that.doLoadData();
  },
  addBtnTap: function() {
    var that = this;
    wx.navigateTo({
      url: '../document/submit'
    })
  },
  //按下事件开始  
  doTouchstart: function(e) {
    let that = this;

    var index = e.currentTarget.dataset.index;
    var id = that.data.dataList[index].id;
    // var title = that.data.dataList[index].title;
    console.log(id);

    that.setData({
      isTouchstart: true,
      isLongtap: false
    })

    setTimeout(function() {
      if (that.data.isTouchstart && !that.data.isTouchmove) {
        that.setData({
          isLongtap: true
        })

        // console.log("到时间了,是长按");

        wx.showActionSheet({
          itemList: ['编辑', '删除'],
          success: function(res) {
            if (!res.cancel) {
              if (res.tapIndex == 0) {
                console.log("编辑");

                wx.navigateTo({
                  url: '../document/submit?id=' + id
                })

              } else if (res.tapIndex == 1) {
                console.log("删除");
                that.doDelete(id);
              } else {

              }
            }
          }
        })

      }
    }, 500);
  },
  //按下事件结束  
  doTouchend: function(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var id = that.data.dataList[index].id;

    that.setData({
      isTouchstart: false
    })

    if (!that.data.isLongtap && !that.data.isTouchmove) {
      console.log("点击");

      wx.navigateTo({
        url: '../document/detail?id=' + id
      })

    }
    that.setData({
      isTouchmove: false
    });

  },
  doTouchmove: function(e) {
    let that = this;
    that.setData({
      isTouchmove: true
    })
    console.log("滑动");
  },
  doDelete: function(id) {
    let that = this;

    var formData = {};
    formData['id'] = id;

    wx.showModal({
      title: '确定删除？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          http.ApiPost(apiUrl.UrlDocumentDelete, formData, function(res) {
            if (res.code == 1) {
              that.doLoadData();
            } else {
              wx.showModal({
                title: res.msg
              });
            }
          });
        }
      }
    });
  }
})